package com.irdstudio.efp.console.api.rest;

import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.console.service.facade.SUserRoleService;
import com.irdstudio.efp.console.service.vo.SUserRoleVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api")
public class SUserRoleController extends AbstractController  {
	
	@Autowired
	@Qualifier("sUserRoleService")
	private SUserRoleService sUserRoleService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 */
	@RequestMapping(value="/s/user/roles", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SUserRoleVO>> querySUserRoleAll(
			@RequestBody SUserRoleVO vo) {
		setUserInfoToVO(vo);
		List<SUserRoleVO> outputVo = sUserRoleService.queryAllOwner(vo);
		// 根据用户角色获取用户权限方案
		/** 【不符合switch规范】
		 * String dataAuthority = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		List<SUserRoleVO> outputVo = null;
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataAuthority);

		//根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sUserRoleService.queryAllOwner(vo);
			break;
	
		default:
			outputVo = sUserRoleService.queryAllOwner(vo);
			break;
		}*/
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 */
	@RequestMapping(value="/s/user/role/{userCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SUserRoleVO> queryByPk(@PathVariable("userCode") String userCode) {		
		SUserRoleVO inVo = new SUserRoleVO();
				inVo.setUserCode(userCode);
		SUserRoleVO outputVo = sUserRoleService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}

    /**
     * 根据主键查询详情
     */
    @RequestMapping(value="/s/user/role/list/{userCode}", method=RequestMethod.GET)
    public @ResponseBody ResponseData<List<SUserRoleVO>> queryList(@PathVariable("userCode") String userCode) {
        SUserRoleVO inVo = new SUserRoleVO();
        inVo.setUserCode(userCode);
        List<SUserRoleVO> outputVo = sUserRoleService.queryList(inVo);
        return getResponseData(outputVo);

    }
	
	/**
	 * 根据主键删除信息
	 */
	@RequestMapping(value="/s/user/role", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SUserRoleVO inSUserRoleVo) {		
		int outputVo = sUserRoleService.deleteByPk(inSUserRoleVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 */
	@RequestMapping(value="/s/user/role", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SUserRoleVO inSUserRoleVo) {		
		int outputVo = sUserRoleService.updateByPk(inSUserRoleVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 */
	@RequestMapping(value="/s/user/role", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSUserRole(@RequestBody SUserRoleVO inSUserRoleVo) {
		int outputVo = sUserRoleService.insertSUserRole(inSUserRoleVo);
		return getResponseData(outputVo);
		
	}

	/**
	 * 根据用户编码查询用户权限
	 */
	@RequestMapping(value="/s/user/role/by/{userCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<String> queryRoleListByUserCode(@PathVariable("userCode") String userCode) {
		SUserRoleVO inVo = new SUserRoleVO();
		String right = "0000000";
		List<String> roles = new ArrayList<>();
		inVo.setUserCode(userCode);
		List<SUserRoleVO> outputVo = sUserRoleService.queryList(inVo);
		if (Objects.isNull(outputVo) || outputVo.isEmpty()){
			return ResponseData.create(right, ApplicationConstance.SUCCESS,"查询用户角色为空",true);
		}
		outputVo.forEach(sUserRoleVO -> {
			roles.add(sUserRoleVO.getRoleCode());
		});
		if (roles.contains("R0063")){
			// 影像系统下载权限
			right = "0000101";
		}else if (roles.contains("R0064")){
			// 影像系统查阅权限
			right = "0100001";
		}else if (roles.contains("R0065")){
			// 影像系统下载&查阅权限
			right = "0100101";
		}
		return ResponseData.create(right, ApplicationConstance.SUCCESS,"查询影像系统访问权限成功",true);

	}
}
